﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class game : MonoBehaviour {
	GameObject[] fCube = new GameObject[9];
	
	List<GameObject> Front = new List<GameObject> ();
	List<GameObject> Top = new List<GameObject> ();
	List<GameObject> Back = new List<GameObject> ();
	List<GameObject> Down = new List<GameObject> ();
	List<GameObject> Left = new List<GameObject> ();
	List<GameObject> Right = new List<GameObject> ();

	List<GameObject> Aux = new List<GameObject> ();
	
	List<GameObject> White = new List<GameObject> ();
	List<GameObject> Red = new List<GameObject> ();
	List<GameObject> Yellow = new List<GameObject> ();
	List<GameObject> Orange = new List<GameObject> ();
	List<GameObject> Blue = new List<GameObject> ();
	List<GameObject> Green = new List<GameObject> ();

	int shuffleCount = 0;
	string posicao = "";
	GameObject CameraPivotRotation;
	Camera GameCamera;
	double startFrame;
	double stopFrame = 0;
	double randFrame = 0;
	int move = 0;
	int count = 0;
	Vector3 zeroes;
	GameObject piece;
	//Transform face;
	string objectname;
	Transform objecthit;
	Ray ray;
	RaycastHit hit;
	string side = "";
	//int cubePosX = 0;
	//int cubePosY = 0;
	//int rand = 0;
	public static bool gameStarted = false;
	bool enableShuffle = true;
	// Use this for initialization
	void Start () {
		zeroes.x = 0;
		zeroes.y = 0;
		zeroes.z = 0;
//Fixed Faces
		fCube[0] = GameObject.Find("RCUBE");

		White.Add(GameObject.Find("RCUBE/WHITE"));
		White.Add(GameObject.Find("RCUBE/1"));
		White.Add(GameObject.Find("RCUBE/2"));
		White.Add(GameObject.Find("RCUBE/3"));
		White.Add(GameObject.Find("RCUBE/4"));
		White.Add(GameObject.Find("RCUBE/5"));
		White.Add(GameObject.Find("RCUBE/6"));
		White.Add(GameObject.Find("RCUBE/7"));
		White.Add(GameObject.Find("RCUBE/8"));

		Red.Add(GameObject.Find("RCUBE/RED"));
		Red.Add(GameObject.Find("RCUBE/9"));
		Red.Add(GameObject.Find("RCUBE/10"));
		Red.Add(GameObject.Find("RCUBE/11"));
		Red.Add(GameObject.Find("RCUBE/12"));
		Red.Add(GameObject.Find("RCUBE/13"));
		Red.Add(GameObject.Find("RCUBE/1"));
		Red.Add(GameObject.Find("RCUBE/2"));
		Red.Add(GameObject.Find("RCUBE/3"));

		Yellow.Add(GameObject.Find("RCUBE/YELLOW"));
		Yellow.Add(GameObject.Find("RCUBE/14"));
		Yellow.Add(GameObject.Find("RCUBE/15"));
		Yellow.Add(GameObject.Find("RCUBE/16"));
		Yellow.Add(GameObject.Find("RCUBE/17"));
		Yellow.Add(GameObject.Find("RCUBE/18"));
		Yellow.Add(GameObject.Find("RCUBE/9"));
		Yellow.Add(GameObject.Find("RCUBE/10"));
		Yellow.Add(GameObject.Find("RCUBE/11"));

		Orange.Add(GameObject.Find("RCUBE/ORANGE"));
		Orange.Add(GameObject.Find("RCUBE/6"));
		Orange.Add(GameObject.Find("RCUBE/7"));
		Orange.Add(GameObject.Find("RCUBE/8"));
		Orange.Add(GameObject.Find("RCUBE/19"));
		Orange.Add(GameObject.Find("RCUBE/20"));
		Orange.Add(GameObject.Find("RCUBE/14"));
		Orange.Add(GameObject.Find("RCUBE/15"));
		Orange.Add(GameObject.Find("RCUBE/16"));

		Blue.Add(GameObject.Find("RCUBE/BLUE"));
		Blue.Add(GameObject.Find("RCUBE/9"));
		Blue.Add(GameObject.Find("RCUBE/12"));
		Blue.Add(GameObject.Find("RCUBE/1"));
		Blue.Add(GameObject.Find("RCUBE/17"));
		Blue.Add(GameObject.Find("RCUBE/4"));
		Blue.Add(GameObject.Find("RCUBE/14"));
		Blue.Add(GameObject.Find("RCUBE/19"));
		Blue.Add(GameObject.Find("RCUBE/6"));

		Green.Add(GameObject.Find("RCUBE/GREEN"));
		Green.Add(GameObject.Find("RCUBE/3"));
		Green.Add(GameObject.Find("RCUBE/13"));
		Green.Add(GameObject.Find("RCUBE/11"));
		Green.Add(GameObject.Find("RCUBE/5"));
		Green.Add(GameObject.Find("RCUBE/18"));
		Green.Add(GameObject.Find("RCUBE/8"));
		Green.Add(GameObject.Find("RCUBE/20"));
		Green.Add(GameObject.Find("RCUBE/16"));

		Front = White;
		Top = Red;
		Back = Yellow;
		Down = Orange;
		Left = Blue;
		Right = Green;

		CameraPivotRotation = GameObject.Find ("CameraPivot");
		GameCamera = Camera.current;
		GameCamera.transform.parent = CameraPivotRotation.transform;
		randFrame = Time.frameCount;
	}
	// Update is called once per frame
	void Update () {
//		OnGUI ();
		if (!gameStarted) {
			if (Time.frameCount > randFrame + 10){
				shuffleCount++;
				//rand = ;
				switch(Random.Range(0,11)){
				case 0: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "R"; 
					for (int i = 1; i <= 8; i++) {Right[i].transform.parent = Right[0].transform;}; break;
				case 1: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "R"; 
					for (int i = 1; i <= 8; i++) {Right[i].transform.parent = Right[0].transform;}; break;
				case 2: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "L"; 
					for (int i = 1; i <= 8; i++) {Left[i].transform.parent = Left[0].transform;}; break;
				case 3: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "L"; 
					for (int i = 1; i <= 8; i++) {Left[i].transform.parent = Left[0].transform;}; break;
				case 4: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "F"; 
					for (int i = 1; i <= 8; i++) {Front[i].transform.parent = Front[0].transform;}; break;
				case 5: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "F"; 
					for (int i = 1; i <= 8; i++) {Front[i].transform.parent = Front[0].transform;}; break;
				case 6: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "B"; 
					for (int i = 1; i <= 8; i++) {Back[i].transform.parent = Back[0].transform;}; break;
				case 7: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "B"; 
					for (int i = 1; i <= 8; i++) {Back[i].transform.parent = Back[0].transform;}; break;
				case 8: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "T"; 
					for (int i = 1; i <= 8; i++) {Top[i].transform.parent = Top[0].transform;}; break;
				case 9: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "T"; 
					for (int i = 1; i <= 8; i++) {Top[i].transform.parent = Top[0].transform;}; break;
				case 10: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 1; side = "D"; 
					for (int i = 1; i <= 8; i++) {Down[i].transform.parent = Down[0].transform;}; break;
				case 11: startFrame = Time.frameCount; stopFrame = startFrame + 10; count = 10; move = 2; side = "D"; 
					for (int i = 1; i <= 8; i++) {Down[i].transform.parent = Down[0].transform;}; break;
				}
				randFrame = Time.frameCount;
				if (shuffleCount > 20){
					gameStarted = true;
					shuffleCount = 0;
					enableShuffle = true;
				}
			}
		} else {
//			if (enableShuffle == true){
//				ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//				hit = new RaycastHit ();
//				if (Physics.Raycast (ray, out hit, 200)) {
//					objectname = hit.transform.name;
//					if (objectname.Equals("shuffleButton")){
//						objectname = "";
//						gameStarted = false;
//					}
//				}
//			}
			for (var i = 0; i < Input.touchCount; ++i) {
				Touch touch = Input.GetTouch (i);
				if (touch.phase == TouchPhase.Began) {
					if (touch.position.x > (Screen.width / 2)) {
						posicao = "direita";
					} else if (touch.position.x < (Screen.width / 2)) {
						posicao = "esquerda";
					}
				}
			}
			if (SwipeManager.swipeDirection == Swipe.Up && count == 0) {
				startFrame = Time.frameCount;
				stopFrame = startFrame + 10;
				count = 10;
				move = 1;
				side = checkSide();
			}
			if (SwipeManager.swipeDirection == Swipe.Down && count == 0) {
				startFrame = Time.frameCount;
				stopFrame = startFrame + 10;
				count = 10;
				move = 2;
				side = checkSide();
			}
			if (SwipeManager.swipeDirection == Swipe.Right && count == 0) {
				startFrame = Time.frameCount;
				stopFrame = startFrame + 10;
				count = 10;
				move = 3;
				side = checkSide();
			}
			if (SwipeManager.swipeDirection == Swipe.Left && count == 0) {
				startFrame = Time.frameCount;
				stopFrame = startFrame + 10;
				count = 10;
				move = 4;
				side = checkSide();
			}
		}

		if (Time.frameCount < stopFrame) {
			rotate(side);
		}
	}
	//--------------------- Shuffle -----------------------
	//------------------ Camera ---------------------------
	void RotateCameraRight(){
//		cubePosX++;
//		if (cubePosX == 40) {
//			cubePosX = 0;
//		}
		CameraPivotRotation.transform.Rotate(Vector3.down * -9);
	}
	void RotateCameraLeft(){
//		cubePosX--;
//		if (cubePosX == -40) {
//			cubePosX = 0;
//		}
		CameraPivotRotation.transform.Rotate(Vector3.up * -9);
	}
	void RotateCameraDown(){
//		cubePosY--;
//		if (cubePosY == -40) {
//			cubePosY = 0;
//		}
		CameraPivotRotation.transform.Rotate(Vector3.forward * -9);
	}
	void RotateCameraUp(){
//		cubePosY++;
//		if (cubePosY == 40) {
//			cubePosY = 0;
//		}
		CameraPivotRotation.transform.Rotate(Vector3.back * -9);
	}
//------------------ Check --------------------------- 
	string checkSide(){
		ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		hit = new RaycastHit ();
		if (Physics.Raycast (ray, out hit, 200)) {
			objectname = hit.transform.name;
			objecthit = hit.transform as Transform;
			piece = hit.transform.parent.gameObject;

//		if (cubePosY == 0){
			if (Left.Contains(piece)){
				if (Left[1] == piece || Left[6] == piece){
					if (posicao.Equals("direita")){
						if (move == 1 || move == 2){
							for (int i = 1; i <= 8; i++) {
								Back[i].transform.parent = Back[0].transform;
							}
							return "B";
						}
					}
					if (Left[1] == piece && move == 4){
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Left[6] == piece && move == 4){
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}	
				} else if (Left[3] == piece || Left[8] == piece){
					if (posicao.Equals("esquerda")){
						if (move == 1 || move == 2){
							for (int i = 1; i <= 8; i++) {
								Front[i].transform.parent = Front[0].transform;
							}
							return "F";
						}
					}
					if (Left[3] == piece && move == 3){
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Left[8] == piece && move == 3){
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}
				}
			}

			if (Front.Contains(piece)){
				if (Front[1] == piece || Front[6] == piece){
					if (posicao.Equals("direita")){
						if (move == 1 || move == 2){
							for (int i = 1; i <= 8; i++) {
								Left[i].transform.parent = Left[0].transform;
							}
							return "L";
						}
					}
					if (Front[1] == piece && move == 4){
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Front[6] == piece && move == 4){
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}	
				} else if (Front[3] == piece || Front[8] == piece){
					if (posicao.Equals("esquerda")){
						if (move == 1 || move == 2){
							for (int i = 1; i <= 8; i++) {
								Right[i].transform.parent = Right[0].transform;
							}
							return "R";
						}
					}
					if (Front[3] == piece && move == 3){
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Front[8] == piece && move == 3){
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}
				}
			}

			if (Back.Contains(piece)){
				if (Back[1] == piece || Back[6] == piece){
					if (posicao.Equals("esquerda")){ //Test
						if (move == 1 || move == 2){
							if (move == 1){
								move = 2;
							} else if (move == 2){
								move = 1;
							}
							for (int i = 1; i <= 8; i++) {
								Left[i].transform.parent = Left[0].transform;
							}
							return "L";
						}
					}
					if (Back[1] == piece && move == 3){ //4
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}	
					if (Back[6] == piece && move == 3){ //4
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
				} else if (Back[3] == piece || Back[8] == piece){
					if (posicao.Equals("direita")){ //Test
						if (move == 1 || move == 2){
							if (move == 1){
								move = 2;
							} else if (move == 2){
								move = 1;
							}
							for (int i = 1; i <= 8; i++) {
								Right[i].transform.parent = Right[0].transform;
							}
							return "R";
						}
					}
					if (Back[3] == piece && move == 4){ //4
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}	
					if (Back[8] == piece && move == 4){ //4
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}
				}
			}

			if (Right.Contains(piece)){ //----------------------------
				if (Right[1] == piece || Right[6] == piece){
					if (posicao.Equals("direita")){
						if (move == 1 || move == 2){
							if (move == 1){
								move = 2;
							} else if (move == 2){
								move = 1;
							}
							for (int i = 1; i <= 8; i++) {
								Front[i].transform.parent = Front[0].transform;
							}
							return "F";
						}
					}
					if (Right[1] == piece && move == 4){ //4
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Right[6] == piece && move == 4){ //4
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}
				} else if (Right[3] == piece || Right[8] == piece){
					if (posicao.Equals("esquerda")){
						if (move == 1 || move == 2){
							if (move == 1){
								move = 2;
							} else if (move == 2){
								move = 1;
							}
							for (int i = 1; i <= 8; i++) {
								Back[i].transform.parent = Back[0].transform;
							}
							return "B";
						}
					}
					if (Right[3] == piece && move == 3){ //3
						for (int i = 1; i <= 8; i++) {
							Top[i].transform.parent = Top[0].transform;
						}
						return "T";
					}	
					if (Right[8] == piece && move == 3){ //3
						for (int i = 1; i <= 8; i++) {
							Down[i].transform.parent = Down[0].transform;
						}
						return "D";
					}
				}
			}
//			}
		}
		return "";
	}
//------------------ Rotate --------------------------- 
	void rotate(string sides){
		if (sides.Equals("F")){
			if (move == 1){
				count--;
				rotateFront(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetFrontClock();
				}
			} else if (move == 2){
				count--;
				rotateFront(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetFrontAntiClock();
				}
			} else if (move == 3){
				count--;
				rotateFront(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetFrontClock();
				}
			} else if (move == 4){
				count--;
				rotateFront(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetFrontAntiClock();
				}
			}
		}
		if (sides.Equals("B")){
			if (move == 1){
				count--;
				rotateBack(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetBackClock();
				}
			} else if (move == 2){
				count--;
				rotateBack(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetBackAntiClock();
				}
			} else if (move == 3){
				count--;
				rotateBack(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetBackAntiClock();
				}
			} else if (move == 4){
				count--;
				rotateBack(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetBackClock();
				}
			}
		}
		if (sides.Equals("R")){
			if (move == 1){
				count--;
				rotateRight(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetRightClock();
				}
			} else if (move == 2){
				count--;
				rotateRight(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetRightAntiClock();
				}
			} else if (move == 3){
				count--;
				rotateRight(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetRightAntiClock();
				}
			} else if (move == 4){
				count--;
				rotateRight(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetRightClock();
				}
			}
		}
		if (sides.Equals("L")){
			if (move == 1){
				count--;
				rotateLeft(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetLeftAntiClock();
				}
			} else if (move == 2){
				count--;
				rotateLeft(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetLeftClock();
				}
			} else if (move == 3){
				count--;
				rotateLeft(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetLeftClock();
				}
			} else if (move == 4){
				count--;
				rotateLeft(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetLeftAntiClock();
				}
			}
		}
		if (sides.Equals("T")){
			if (move == 1){
				count--;
				rotateTop(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetTopClock();
				}
			} else if (move == 2){
				count--;
				rotateTop(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetTopAntiClock();
				}
			} else if (move == 3){
				count--;
				rotateTop(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetTopAntiClock();
				}
			} else if (move == 4){
				count--;
				rotateTop(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetTopClock();
				}
			}
		}
		if (sides.Equals("D")){
			if (move == 1){
				count--;
				rotateDown(1);
				if (count == 0){
					move = 0; 
					side = "";
					resetDownClock();
				}
			} else if (move == 2){
				count--;
				rotateDown(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetDownAntiClock();
				}
			} else if (move == 3){  //rotateDown(0);    //resetDownAntiClock();
				count--;
				rotateDown(1);  
				if (count == 0){
					move = 0; 
					side = "";
					resetDownClock();
				}
			} else if (move == 4){ //rotateDown(1);  //resetDownClock();
				count--;
				rotateDown(0);
				if (count == 0){
					move = 0; 
					side = "";
					resetDownAntiClock();
				}
			}
		}
		if (sides.Equals("")){
			switch(move){
			case 1: count--; RotateCameraUp(); if (count == 0){move = 0;}; break;
			case 2: count--; RotateCameraDown(); if (count == 0){move = 0;}; break;
			case 3: count--; RotateCameraRight(); if (count == 0){move = 0;}; break;
			case 4: count--; RotateCameraLeft(); if (count == 0){move = 0;}; break;
			}
		}
	}
//------------------ Front --------------------------- //White
	void rotateFront(int direction){
		if (direction == 1) {
			Front [0].transform.Rotate (Vector3.right * -9); //right
		} else if (direction == 0) {
			Front [0].transform.Rotate (Vector3.left * -9);	//left
		}
	}
//------------------ Back ---------------------------	//Yellow
	void rotateBack(int direction){
		if (direction == 1) {
			Back [0].transform.Rotate (Vector3.right * -9);	//right
		} else if (direction == 0) {
			Back [0].transform.Rotate (Vector3.left * -9);	//left
		}
	}
//------------------ Right --------------------------- //Green
	void rotateRight(int direction){
		if (direction == 1) {
			Right [0].transform.Rotate (Vector3.forward * -9);	//forward
		} else if (direction == 0) {
			Right [0].transform.Rotate (Vector3.back * -9);	//back
		}
	}
//------------------ Left --------------------------- //Blue
	void rotateLeft(int direction){
		if (direction == 1) {
			Left [0].transform.Rotate (Vector3.forward * -9);	//forward	
		} else if (direction == 0) {
			Left [0].transform.Rotate (Vector3.back * -9);	//back	
		}
	}
//------------------ Top --------------------------- //Red
	void rotateTop(int direction){
		if (direction == 1) {
			Top [0].transform.Rotate (Vector3.down * -9); //down	
		} else if (direction == 0) {
			Top [0].transform.Rotate (Vector3.up * -9);	//up
		}
	}
//------------------ Down --------------------------- //Orange
	void rotateDown(int direction){
		if (direction == 1) {
			Down [0].transform.Rotate (Vector3.up * -9); //down		
		} else if (direction == 0) {
			Down [0].transform.Rotate (Vector3.down * -9);	//up	
		}
	}
//------------------ Reset Cube ---------------------------
	void resetFrontClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Front[i]);
		}
		Front.Clear();
		Front.Insert(0,Aux[0]);
		Front.Insert(1,Aux[6]);
		Front.Insert(2,Aux[4]);
		Front.Insert(3,Aux[1]);
		Front.Insert(4,Aux[7]);
		Front.Insert(5,Aux[2]);
		Front.Insert(6,Aux[8]);
		Front.Insert(7,Aux[5]);
		Front.Insert(8,Aux[3]);
		Aux.Clear();
		Down[1] = Front[6];
		Down[2] = Front[7];
		Down[3] = Front[8];
		Right[1] = Front[3];
		Right[4] = Front[5];
		Right[6] = Front[8];
		Top[6] = Front[1];
		Top[7] = Front[2];
		Top[8] = Front[3];
		Left[3] = Front[1];
		Left[5] = Front[4];
		Left[8] = Front[6];
	}
	void resetFrontAntiClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Front[i]);
		}
		Front.Clear();
		Front.Insert(0,Aux[0]);
		Front.Insert(1,Aux[3]);
		Front.Insert(2,Aux[5]);
		Front.Insert(3,Aux[8]);
		Front.Insert(4,Aux[2]);
		Front.Insert(5,Aux[7]);
		Front.Insert(6,Aux[1]);
		Front.Insert(7,Aux[4]);
		Front.Insert(8,Aux[6]);
		Aux.Clear();
		Down[1] = Front[6];
		Down[2] = Front[7];
		Down[3] = Front[8];
		Right[1] = Front[3];
		Right[4] = Front[5];
		Right[6] = Front[8];
		Top[6] = Front[1];
		Top[7] = Front[2];
		Top[8] = Front[3];
		Left[3] = Front[1];
		Left[5] = Front[4];
		Left[8] = Front[6];
	}
	void resetBackAntiClock(){ //Clock
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Back[i]);
		}
		Back.Clear();
		Back.Insert(0,Aux[0]);
		Back.Insert(1,Aux[6]);
		Back.Insert(2,Aux[4]);
		Back.Insert(3,Aux[1]);
		Back.Insert(4,Aux[7]);
		Back.Insert(5,Aux[2]);
		Back.Insert(6,Aux[8]);
		Back.Insert(7,Aux[5]);
		Back.Insert(8,Aux[3]);
		Aux.Clear();
		Down[6] = Back[1];
		Down[7] = Back[2];
		Down[8] = Back[3];
		Right[3] = Back[8];
		Right[5] = Back[5];
		Right[8] = Back[3];
		Top[1] = Back[6];
		Top[2] = Back[7];
		Top[3] = Back[8];
		Left[1] = Back[6];
		Left[4] = Back[4];
		Left[6] = Back[1];
	}
	void resetBackClock(){ //AntiClock
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Back[i]);
		}
		Back.Clear();
		Back.Insert(0,Aux[0]);
		Back.Insert(1,Aux[3]);
		Back.Insert(2,Aux[5]);
		Back.Insert(3,Aux[8]);
		Back.Insert(4,Aux[2]);
		Back.Insert(5,Aux[7]);
		Back.Insert(6,Aux[1]);
		Back.Insert(7,Aux[4]);
		Back.Insert(8,Aux[6]);
		Aux.Clear();
		Down[6] = Back[1];
		Down[7] = Back[2];
		Down[8] = Back[3];
		Right[3] = Back[8];
		Right[5] = Back[5];
		Right[8] = Back[3];
		Top[1] = Back[6];
		Top[2] = Back[7];
		Top[3] = Back[8];
		Left[1] = Back[6];
		Left[4] = Back[4];
		Left[6] = Back[1];
	}
	void resetLeftClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Left[i]);
		}
		Left.Clear();
		Left.Insert(0,Aux[0]);
		Left.Insert(1,Aux[6]);
		Left.Insert(2,Aux[4]);
		Left.Insert(3,Aux[1]);
		Left.Insert(4,Aux[7]);
		Left.Insert(5,Aux[2]);
		Left.Insert(6,Aux[8]);
		Left.Insert(7,Aux[5]);
		Left.Insert(8,Aux[3]);
		Aux.Clear();
		Down[1] = Left[8];
		Down[4] = Left[7];
		Down[6] = Left[6];
		Top[1] = Left[1];
		Top[4] = Left[2];
		Top[6] = Left[3];
		Back[1] = Left[6];
		Back[4] = Left[4];
		Back[6] = Left[1];
		Front[1] = Left[3];
		Front[4] = Left[5];
		Front[6] = Left[8];
	}
	void resetLeftAntiClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Left[i]);
		}
		Left.Clear();
		Left.Insert(0,Aux[0]);
		Left.Insert(1,Aux[3]);
		Left.Insert(2,Aux[5]);
		Left.Insert(3,Aux[8]);
		Left.Insert(4,Aux[2]);
		Left.Insert(5,Aux[7]);
		Left.Insert(6,Aux[1]);
		Left.Insert(7,Aux[4]);
		Left.Insert(8,Aux[6]);
		Aux.Clear();
		Down[1] = Left[8];
		Down[4] = Left[7];
		Down[6] = Left[6];
		Top[1] = Left[1];
		Top[4] = Left[2];
		Top[6] = Left[3];
		Back[1] = Left[6];
		Back[4] = Left[4];
		Back[6] = Left[1];
		Front[1] = Left[3];
		Front[4] = Left[5];
		Front[6] = Left[8];
	}
	void resetRightClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Right[i]);
		}
		Right.Clear();
		Right.Insert(0,Aux[0]);
		Right.Insert(1,Aux[6]);
		Right.Insert(2,Aux[4]);
		Right.Insert(3,Aux[1]);
		Right.Insert(4,Aux[7]);
		Right.Insert(5,Aux[2]);
		Right.Insert(6,Aux[8]);
		Right.Insert(7,Aux[5]);
		Right.Insert(8,Aux[3]);
		Aux.Clear();
		Down[3] = Right[6];
		Down[5] = Right[7];
		Down[8] = Right[8];
		Top[3] = Right[3];
		Top[5] = Right[2];
		Top[8] = Right[1];
		Back[3] = Right[8];
		Back[5] = Right[5];
		Back[8] = Right[3];
		Front[3] = Right[1];
		Front[5] = Right[4];
		Front[8] = Right[6];
	}
	void resetRightAntiClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Right[i]);
		}
		Right.Clear();
		Right.Insert(0,Aux[0]);
		Right.Insert(1,Aux[3]);
		Right.Insert(2,Aux[5]);
		Right.Insert(3,Aux[8]);
		Right.Insert(4,Aux[2]);
		Right.Insert(5,Aux[7]);
		Right.Insert(6,Aux[1]);
		Right.Insert(7,Aux[4]);
		Right.Insert(8,Aux[6]);
		Aux.Clear();
		Down[3] = Right[6];
		Down[5] = Right[7];
		Down[8] = Right[8];
		Top[3] = Right[3];
		Top[5] = Right[2];
		Top[8] = Right[1];
		Back[3] = Right[8];
		Back[5] = Right[5];
		Back[8] = Right[3];
		Front[3] = Right[1];
		Front[5] = Right[4];
		Front[8] = Right[6];
	}
	void resetTopClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Top[i]);
		}
		Top.Clear();
		Top.Insert(0,Aux[0]);
		Top.Insert(1,Aux[6]);
		Top.Insert(2,Aux[4]);
		Top.Insert(3,Aux[1]);
		Top.Insert(4,Aux[7]);
		Top.Insert(5,Aux[2]);
		Top.Insert(6,Aux[8]);
		Top.Insert(7,Aux[5]);
		Top.Insert(8,Aux[3]);
		Aux.Clear();
		Front[1] = Top[6];
		Front[2] = Top[7];
		Front[3] = Top[8];
		Back[6] = Top[1];
		Back[7] = Top[2];
		Back[8] = Top[3];
		Right[1] = Top[8];
		Right[2] = Top[5];
		Right[3] = Top[3];
		Left[1] = Top[1];
		Left[2] = Top[4];
		Left[3] = Top[6];
	}
	void resetTopAntiClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Top[i]);
		}
		Top.Clear();
		Top.Insert(0,Aux[0]);
		Top.Insert(1,Aux[3]);
		Top.Insert(2,Aux[5]);
		Top.Insert(3,Aux[8]);
		Top.Insert(4,Aux[2]);
		Top.Insert(5,Aux[7]);
		Top.Insert(6,Aux[1]);
		Top.Insert(7,Aux[4]);
		Top.Insert(8,Aux[6]);
		Aux.Clear();
		Front[1] = Top[6];
		Front[2] = Top[7];
		Front[3] = Top[8];
		Back[6] = Top[1];
		Back[7] = Top[2];
		Back[8] = Top[3];
		Right[1] = Top[8];
		Right[2] = Top[5];
		Right[3] = Top[3];
		Left[1] = Top[1];
		Left[2] = Top[4];
		Left[3] = Top[6];
	}
	void resetDownClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Down[i]);
		}
		Down.Clear();
		Down.Insert(0,Aux[0]);
		Down.Insert(1,Aux[6]);
		Down.Insert(2,Aux[4]);
		Down.Insert(3,Aux[1]);
		Down.Insert(4,Aux[7]);
		Down.Insert(5,Aux[2]);
		Down.Insert(6,Aux[8]);
		Down.Insert(7,Aux[5]);
		Down.Insert(8,Aux[3]);
		Aux.Clear();
		Front[6] = Down[1];
		Front[7] = Down[2];
		Front[8] = Down[3];
		Back[1] = Down[6];
		Back[2] = Down[7];
		Back[3] = Down[8];
		Right[6] = Down[3];
		Right[7] = Down[5];
		Right[8] = Down[8];
		Left[6] = Down[6];
		Left[7] = Down[4];
		Left[8] = Down[1];
	}
	void resetDownAntiClock(){
		for (int i = 0; i < 9; i ++) {
			Aux.Insert(i,Down[i]);
		}
		Down.Clear();
		Down.Insert(0,Aux[0]);
		Down.Insert(1,Aux[3]);
		Down.Insert(2,Aux[5]);
		Down.Insert(3,Aux[8]);
		Down.Insert(4,Aux[2]);
		Down.Insert(5,Aux[7]);
		Down.Insert(6,Aux[1]);
		Down.Insert(7,Aux[4]);
		Down.Insert(8,Aux[6]);
		Aux.Clear();
		Front[6] = Down[1];
		Front[7] = Down[2];
		Front[8] = Down[3];
		Back[1] = Down[6];
		Back[2] = Down[7];
		Back[3] = Down[8];
		Right[6] = Down[3];
		Right[7] = Down[5];
		Right[8] = Down[8];
		Left[6] = Down[6];
		Left[7] = Down[4];
		Left[8] = Down[1];
	}
	void resetCube(){
//		for (int i = 0; i <= 8; i++) {
//			White[i].transform.parent = null;
//			Red[i].transform.parent = null;
//			Yellow[i].transform.parent = null;
//			Orange[i].transform.parent = null;
//			Blue[i].transform.parent = null;
//			Green[i].transform.parent = null;
//		}
//		fCube [0].transform.eulerAngles = zeroes;
//		for (int i = 0; i <= 8; i++) {
//			White[i].transform.parent = fCube [0].transform;
//			Red[i].transform.parent = fCube [0].transform;
//			Yellow[i].transform.parent = fCube [0].transform;
//			Orange[i].transform.parent = fCube [0].transform;
//			Blue[i].transform.parent = fCube [0].transform;
//			Green[i].transform.parent = fCube [0].transform;
//		}
	}
//	void OnGUI(){
////		if (GUI.Button (new Rect (15, 15, 400, 100), "Embaralhar")) {
////			gameStarted = false;
////		}
//
////		string white = "";
////		for (int i = 0; i < 9; i++) {
////			white += Front[i].name+"\n";
////		}
////		string yellow = "";
////		for (int i = 0; i < 9; i++) {
////			yellow += Back[i].name+"\n";
////		}
////		string red = "";
////		for (int i = 0; i < 9; i++) {
////			red += Top[i].name+"\n";
////		}
////		string orange = "";
////		for (int i = 0; i < 9; i++) {
////			orange += Down[i].name+"\n";
////		}
////		string blue = "";
////		for (int i = 0; i < 9; i++) {
////			blue += Left[i].name+"\n";
////		}
////		string green = "";
////		for (int i = 0; i < 9; i++) {
////			green += Right[i].name+"\n";
////		}
////		string objsTouched = "";
////		for (int i = 0; i < Touched.Count; i++) {
////			objsTouched += Touched[i].name+"\t";
////		}
//		GUI.Label(new Rect(10, 10, 1900, 1000),
////		          "\t"+"Front[1]:"+"\t"+Front[3].name
////		          +"\t"+"Front[4]:"+"\t"+Front[5].name
////		          +"\t"+"Front[6]:"+"\t"+Front[8].name+"\n"
////		          +"\t"+"Right[3]:"+"\t"+Right[1].name
////		          +"\t"+"Right[5]:"+"\t"+Right[4].name
////		          +"\t"+"Right[8]:"+"\t"+Right[6].name+"\n"
////		          "\t"+white+"\t"+yellow+"\t"+red+"\t"+orange+"\t"+blue+"\t"+green
////		          "Pos X: " + cubePosX.ToString() + " Pos Y: " + cubePosY.ToString()
//		          ""
//		          );
//	}
}